package movieGandalf.DB;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MovieTools {
	
	public static Integer getLanguageID(Connection conn, String language){
		PreparedStatement getLanguageID = null;
		Object o = null;
		Integer languageID = null;
		
		try{
			getLanguageID = conn.prepareStatement("SELECT languageID FROM Languages WHERE language=?");
			getLanguageID.setString(1, language);
			ResultSet rs = getLanguageID.executeQuery();
			if (rs.next()){
				o = rs.getObject("languageID");
				if (!(o instanceof Integer))
					return null;
				languageID = (Integer) o;
			}
			getLanguageID.close();
		}
	    catch (Exception e){
	    	return null;
	    }
		
		return languageID;
	}
	
	public static Integer getCountryID(Connection conn, String country){
		PreparedStatement getCountryID = null;
		Object o = null;
		Integer countryID = null;
		
		try{
			getCountryID = conn.prepareStatement("SELECT countryID FROM Countries WHERE country=?");
			getCountryID.setString(1, country);
			ResultSet rs = getCountryID.executeQuery();
			if (rs.next()){
				o = rs.getObject("countryID");
				if (!(o instanceof Integer))
					return null;
				countryID = (Integer) o;
			}
			getCountryID.close();
		}
	    catch (Exception e){
	    	return null;
	    }
		
		return countryID;
	}
	
	public static BigInteger getDirectorID(Connection conn, String directorName){
		PreparedStatement getDirectorID = null;
		Object o = null;
		BigInteger directorID = null;
		
		try{
			getDirectorID = conn.prepareStatement("SELECT directorID FROM Directors WHERE directorName=?");
			getDirectorID.setString(1, directorName);
			ResultSet rs = getDirectorID.executeQuery();
			if (rs.next()){
				o = rs.getObject("directorID");
				if (!(o instanceof BigInteger))
					return null;
				directorID = (BigInteger) o;
			}
			getDirectorID.close();
		}
	    catch (Exception e){
	    	return null;
	    }
		
		return directorID;
	}
	
	public static BigInteger getActorID(Connection conn, String actorName){
		PreparedStatement getActorID = null;
		Object o = null;
		BigInteger actorID = null;
		
		try{
			getActorID = conn.prepareStatement("SELECT actorID FROM Actors WHERE actorName=?");
			getActorID.setString(1, actorName);
			ResultSet rs = getActorID.executeQuery();
			if (rs.next()){
				o = rs.getObject("actorID");
				if (!(o instanceof BigInteger))
					return null;
				actorID = (BigInteger) o;
			}
			getActorID.close();
		}
	    catch (Exception e){
	    	return null;
	    }
		
		return actorID;
	}
	
	public static Integer getGenreID(Connection conn, String genre){
		PreparedStatement getGenreID = null;
		Object o = null;
		Integer genreID = null;
		
		try{
			getGenreID = conn.prepareStatement("SELECT genreID FROM Genres WHERE genre=?");
			getGenreID.setString(1, genre);
			ResultSet rs = getGenreID.executeQuery();
			if (rs.next()){
				o = rs.getObject("genreID");
				if (!(o instanceof Integer))
					return null;
				genreID = (Integer) o;
			}
			getGenreID.close();
		}
	    catch (Exception e){
	    	return null;
	    }
		
		return genreID;
	}
	
	public static Integer getMPAARatingID(Connection conn, String mpaaRating){
		PreparedStatement getMPAARatingID = null;
		Object o = null;
		Integer mpaaRatingID = null;
		
		try{
			getMPAARatingID = conn.prepareStatement("SELECT mpaaRatingID FROM MPAARatings WHERE mpaaRating=?");
			getMPAARatingID.setString(1, mpaaRating);
			ResultSet rs = getMPAARatingID.executeQuery();
			if (rs.next()){
				o = rs.getObject("mpaaRatingID");
				if (!(o instanceof Integer))
					return null;
				mpaaRatingID = (Integer) o;
			}
			getMPAARatingID.close();
		}
	    catch (Exception e){
	    	return null;
	    }
		
		return mpaaRatingID;
	}
	
	public static BigInteger getMovieID(Connection conn, String imdbName){
		PreparedStatement getMovieID = null;
		Object o = null;
		BigInteger movieID = null;
		
		try{
			getMovieID = conn.prepareStatement("SELECT movieID FROM Movies WHERE imdbName=?");
			getMovieID.setString(1, imdbName);
			ResultSet rs = getMovieID.executeQuery();
			if (rs.next()){
				o = rs.getObject("movieID");
				if (!(o instanceof BigInteger))
					return null;
				movieID = (BigInteger) o;
			}
			getMovieID.close();
		}
	    catch (Exception e){
	    	return null;
	    }
		
		return movieID;
	}
	
	public static String getLine(InputStream in){
		int i=0,j=0;
		
		char[] charList = new char[100];
		char[] temp = null;
		int size=100;
		int curInt;
		
		char[] chars = null;
		i=0;
		
		try {
			while (in.available()>0){
				 curInt = in.read();
				 char c = (char) curInt;
				 if (c!='\n'){
					 charList[i] = c;
					 i++;
					 if (i%100==0){
						 temp = new char[size];
						 for (j=0;j<size;j++)
							 temp[j]=charList[j];
						 
						 charList = new char[size+100];
						 for (j=0;j<size;j++)
							 charList[j]=temp[j];
						 
						 size+=100;
					 }
				 }
				 else{
					 break;
				 }
			}
			
			if (i==0){
				if (in.available()<=0)
					return null;
				return "";
			}
		}
		catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		
		chars = new char[i];
		
		for (i--;i>=0;i--)
			chars[i] = charList[i];
		
		return String.valueOf(chars);
	}
		
	
	public static String getMovieName(String line){
		int i=0;
		int len = line.length();
		
		for (i=0;i<len;i++){
			if (line.charAt(i)=='\t' || line.charAt(i)=='(')
				break;
		}
		if (i==len)
			return "";
		
		String shortName = line.substring(0,i).trim();
		
		return shortName;
	}
	
	public static String getIMDBName(String line){
		int i=0,j=0;
		int len = line.length();
		int l = 0;
		boolean isNumber = true;
		
		for (i=0;i<len;i++){
			if (line.charAt(i)=='\t')
				break;
		}
		if (i==len)
			return "";
		
		String movie = line.substring(0,i).trim();
		String[] parts = movie.split(" ");
		for (i=parts.length-1;i>=0;i--){
			//if (!parts[i].equals("(V)") && !parts[i].equals("(TV)") && !parts[i].equals("{{SUSPENDED}}") && !parts[i].equals("(uncredited)"))
			l = parts[i].length();
			if (l<2 || parts[i].charAt(0)!='(' || parts[i].charAt(l-1)!=')')
				continue;
			
			isNumber = true;
			for (j=1;j<parts[i].length()-1;j++){
				if (!Character.isDigit(parts[i].charAt(j)) && parts[i].charAt(j)!='?'){
					isNumber = false;
					break;
				}
			}
			
			if (isNumber)
				break;
		}
		
		if (i<0)
			return movie;
		
		String imdbName = "";
		for (j=0;j<=i;j++){
			if (j!=0)
				imdbName += " ";
			imdbName += parts[j];
		}
		
		i = movie.length() - 1;
		while (movie.charAt(i)==')' && i>=1 && !Character.isDigit(movie.charAt(i-1)) && movie.charAt(i-1)!='?'){
			for (;i>=0 && movie.charAt(i)!='(';i--);
			movie = movie.substring(0,i+1);
		}
		
		return imdbName;
	}
	
	public static Short getYear(String line){
		int i=0;
		int end = line.length() - 1;
		
		for (;end>=0 && !Character.isDigit(line.charAt(end)) && line.charAt(end)!='?';end--);
		
		for (i=end;i>=0 && (Character.isDigit(line.charAt(i)) || line.charAt(i)=='?');i--);
		
		if (i<0)
			return -1;
		
		String year = line.substring(i+1,end+1);
		if (year.contains("?"))
			year = "0";
		
		return new Short(Short.parseShort(year));
	}
}
